﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Spinnerets.Network.Collection
{
    /// <summary>
    /// Defines the base functionality for a device information collector.
    /// </summary>
    public abstract class InformationCollector
    {
        /// <summary>
        /// Executes device information collection.
        /// </summary>
        public abstract CollectionResults Collect();

        /// <summary>
        /// Creates an <see cref="InformationCollector"/> for the specified address.
        /// </summary>
        /// <param name="address">The address.</param>
        /// <returns></returns>
        public static InformationCollector Create(IPAddress address)
        {
            InformationCollector instance = null;

            try
            {
                using (Socket socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Unspecified))
                {
                    // Test for Windows
                    try
                    {
                        socket.Connect(address, 139);
                        instance = new WmiCollector();
                    }
                    catch { }

                    try
                    {
                        if (instance == null)
                        {
                            socket.Connect(address, 22);
                            instance = new SshCollector();
                        }
                    }
                    catch { }

                    try
                    {
                        if (instance == null)
                        {
                            socket.Connect(address, 161);
                            instance = new SnmpCollector();
                        }
                    }
                    catch { }
                }
            }
            catch (Exception ex)
            {
                string fullTrace = ex.ToString();
            }

            return instance;
        }
    }
}

/*
ManagementObjectSearcher searcher =
    new ManagementObjectSearcher("root\\CIMV2",
    "SELECT * FROM Win32_ComputerSystem");

foreach (ManagementObject queryObj in searcher.Get())
{
    Console.WriteLine("-----------------------------------");
    Console.WriteLine("Win32_ComputerSystem instance");
    Console.WriteLine("-----------------------------------");
    Console.WriteLine("AdminPasswordStatus: {0}", queryObj["AdminPasswordStatus"]);
    Console.WriteLine("AutomaticResetBootOption: {0}", queryObj["AutomaticResetBootOption"]);
    Console.WriteLine("AutomaticResetCapability: {0}", queryObj["AutomaticResetCapability"]);
    Console.WriteLine("BootOptionOnLimit: {0}", queryObj["BootOptionOnLimit"]);
    Console.WriteLine("BootOptionOnWatchDog: {0}", queryObj["BootOptionOnWatchDog"]);
    Console.WriteLine("BootROMSupported: {0}", queryObj["BootROMSupported"]);
    Console.WriteLine("BootupState: {0}", queryObj["BootupState"]);
    Console.WriteLine("Caption: {0}", queryObj["Caption"]);
    Console.WriteLine("ChassisBootupState: {0}", queryObj["ChassisBootupState"]);
    Console.WriteLine("CreationClassName: {0}", queryObj["CreationClassName"]);
    Console.WriteLine("CurrentTimeZone: {0}", queryObj["CurrentTimeZone"]);
    Console.WriteLine("DaylightInEffect: {0}", queryObj["DaylightInEffect"]);
    Console.WriteLine("Description: {0}", queryObj["Description"]);
    Console.WriteLine("Domain: {0}", queryObj["Domain"]);
    Console.WriteLine("DomainRole: {0}", queryObj["DomainRole"]);
    Console.WriteLine("EnableDaylightSavingsTime: {0}", queryObj["EnableDaylightSavingsTime"]);
    Console.WriteLine("FrontPanelResetStatus: {0}", queryObj["FrontPanelResetStatus"]);
    Console.WriteLine("InfraredSupported: {0}", queryObj["InfraredSupported"]);

    if (queryObj["InitialLoadInfo"] == null)
        Console.WriteLine("InitialLoadInfo: {0}", queryObj["InitialLoadInfo"]);
    else
    {
        String[] arrInitialLoadInfo = (String[])(queryObj["InitialLoadInfo"]);
        foreach (String arrValue in arrInitialLoadInfo)
        {
            Console.WriteLine("InitialLoadInfo: {0}", arrValue);
        }
    }
    Console.WriteLine("InstallDate: {0}", queryObj["InstallDate"]);
    Console.WriteLine("KeyboardPasswordStatus: {0}", queryObj["KeyboardPasswordStatus"]);
    Console.WriteLine("LastLoadInfo: {0}", queryObj["LastLoadInfo"]);
    Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
    Console.WriteLine("Model: {0}", queryObj["Model"]);
    Console.WriteLine("Name: {0}", queryObj["Name"]);
    Console.WriteLine("NameFormat: {0}", queryObj["NameFormat"]);
    Console.WriteLine("NetworkServerModeEnabled: {0}", queryObj["NetworkServerModeEnabled"]);
    Console.WriteLine("NumberOfLogicalProcessors: {0}", queryObj["NumberOfLogicalProcessors"]);
    Console.WriteLine("NumberOfProcessors: {0}", queryObj["NumberOfProcessors"]);

    if (queryObj["OEMLogoBitmap"] == null)
        Console.WriteLine("OEMLogoBitmap: {0}", queryObj["OEMLogoBitmap"]);
    else
    {
        Byte[] arrOEMLogoBitmap = (Byte[])(queryObj["OEMLogoBitmap"]);
        foreach (Byte arrValue in arrOEMLogoBitmap)
        {
            Console.WriteLine("OEMLogoBitmap: {0}", arrValue);
        }
    }

    if (queryObj["OEMStringArray"] == null)
        Console.WriteLine("OEMStringArray: {0}", queryObj["OEMStringArray"]);
    else
    {
        String[] arrOEMStringArray = (String[])(queryObj["OEMStringArray"]);
        foreach (String arrValue in arrOEMStringArray)
        {
            Console.WriteLine("OEMStringArray: {0}", arrValue);
        }
    }
    Console.WriteLine("PartOfDomain: {0}", queryObj["PartOfDomain"]);
    Console.WriteLine("PauseAfterReset: {0}", queryObj["PauseAfterReset"]);

    if (queryObj["PowerManagementCapabilities"] == null)
        Console.WriteLine("PowerManagementCapabilities: {0}", queryObj["PowerManagementCapabilities"]);
    else
    {
        UInt16[] arrPowerManagementCapabilities = (UInt16[])(queryObj["PowerManagementCapabilities"]);
        foreach (UInt16 arrValue in arrPowerManagementCapabilities)
        {
            Console.WriteLine("PowerManagementCapabilities: {0}", arrValue);
        }
    }
    Console.WriteLine("PowerManagementSupported: {0}", queryObj["PowerManagementSupported"]);
    Console.WriteLine("PowerOnPasswordStatus: {0}", queryObj["PowerOnPasswordStatus"]);
    Console.WriteLine("PowerState: {0}", queryObj["PowerState"]);
    Console.WriteLine("PowerSupplyState: {0}", queryObj["PowerSupplyState"]);
    Console.WriteLine("PrimaryOwnerContact: {0}", queryObj["PrimaryOwnerContact"]);
    Console.WriteLine("PrimaryOwnerName: {0}", queryObj["PrimaryOwnerName"]);
    Console.WriteLine("ResetCapability: {0}", queryObj["ResetCapability"]);
    Console.WriteLine("ResetCount: {0}", queryObj["ResetCount"]);
    Console.WriteLine("ResetLimit: {0}", queryObj["ResetLimit"]);

    if (queryObj["Roles"] == null)
        Console.WriteLine("Roles: {0}", queryObj["Roles"]);
    else
    {
        String[] arrRoles = (String[])(queryObj["Roles"]);
        foreach (String arrValue in arrRoles)
        {
            Console.WriteLine("Roles: {0}", arrValue);
        }
    }
    Console.WriteLine("Status: {0}", queryObj["Status"]);

    if (queryObj["SupportContactDescription"] == null)
        Console.WriteLine("SupportContactDescription: {0}", queryObj["SupportContactDescription"]);
    else
    {
        String[] arrSupportContactDescription = (String[])(queryObj["SupportContactDescription"]);
        foreach (String arrValue in arrSupportContactDescription)
        {
            Console.WriteLine("SupportContactDescription: {0}", arrValue);
        }
    }
    Console.WriteLine("SystemStartupDelay: {0}", queryObj["SystemStartupDelay"]);

    if (queryObj["SystemStartupOptions"] == null)
        Console.WriteLine("SystemStartupOptions: {0}", queryObj["SystemStartupOptions"]);
    else
    {
        String[] arrSystemStartupOptions = (String[])(queryObj["SystemStartupOptions"]);
        foreach (String arrValue in arrSystemStartupOptions)
        {
            Console.WriteLine("SystemStartupOptions: {0}", arrValue);
        }
    }
    Console.WriteLine("SystemStartupSetting: {0}", queryObj["SystemStartupSetting"]);
    Console.WriteLine("SystemType: {0}", queryObj["SystemType"]);
    Console.WriteLine("ThermalState: {0}", queryObj["ThermalState"]);
    Console.WriteLine("TotalPhysicalMemory: {0}", queryObj["TotalPhysicalMemory"]);
    Console.WriteLine("UserName: {0}", queryObj["UserName"]);
    Console.WriteLine("WakeUpType: {0}", queryObj["WakeUpType"]);
    Console.WriteLine("Workgroup: {0}", queryObj["Workgroup"]);
}
 */